<script type="text/javascript">
//<![CDATA[
var webform_<?php echo $this->getWebform()->getId()?> = new VarienForm('webform_<?php echo $this->getWebform()->getId()?>', <?php echo (int)$this->getData('focus')?>);
var webform_<?php echo $this->getWebform()->getId()?>_submit = function(){
	var form = webform_<?php echo $this->getWebform()->getId()?>;
	if(form.validator && form.validator.validate()){
		form.submit();
		<?php if($this->getAjax()) { ?>
		$('webform_<?php echo $this->getWebform()->getId()?>_submit_button').hide();
		$('webform_<?php echo $this->getWebform()->getId()?>_sending_data').show();
		<?php } ?>
	}
};

<?php if($this->getAjax()) { ?>
var iframe = $('webform_<?php echo $this->getWebform()->getId()?>_iframe');
iframe.observe('load',function(){
	var doc = this.contentDocument ? this.contentDocument: this.contentWindow.document;
	var json = {success:false};
	if(doc.body.innerHTML.unfilterJSON())
		json = doc.body.innerHTML.evalJSON();
	else return;
	if(json.success > 0){
		if(json.script){
			eval(json.script);
			return;
		}
		if(json.redirect_url){
			$('webform_<?php echo $this->getWebform()->getId()?>_progress_text').update('<?php echo $this->__('Redirecting')?>');
			window.location = json.redirect_url;
			return;
		}
		$('webform_<?php echo $this->getWebform()->getId()?>_progress_text').update('<?php echo $this->__('Complete')?>');
		Effect.Fade('webform_<?php echo $this->getWebform()->getId()?>_form',{
			duration: 0.5, from:1, to:0,
			afterFinish: function(){
				$('webform_<?php echo $this->getWebform()->getId()?>_success_text').update(json.success_text.unescapeHTML()).show();
				Effect.Fade('webform_<?php echo $this->getWebform()->getId()?>_success_text',{
					duration:0.5, from:0, to:1
				});
				if(<?php echo (int)$this->getData('scroll_to')?>) $('webform_<?php echo $this->getWebform()->getId()?>_success_text').scrollTo();
			}
			
		});
	} else {
		if($('webform_<?php echo $this->getWebform()->getId()?>_sending_data'))
			$('webform_<?php echo $this->getWebform()->getId()?>_sending_data').hide();
		if($('webform_<?php echo $this->getWebform()->getId()?>_submit_button'))
			$('webform_<?php echo $this->getWebform()->getId()?>_submit_button').show();
		if(json.errors && typeof(json.errors) == "string"){
			Dialog.alert(json.errors.unescapeHTML(),{
				title: "<?php echo $this->__('Error(s) occured')?>",
				className: "alphacube",
				width:300, 
				buttonClass: "button",
				okLabel: "<?php echo $this->__('Close')?>",
				destroyOnClose: true, 
				recenterAuto:true
			})
		} else {
			alert('<?php echo $this->__('Error(s) occured')?>');
		}
		if(json.script){
			eval(json.script);
		}
	}
});
<?php } ?>
//]]>
</script>